package edu.gyc.objecthomework;

/**
 File name: YearsToOvertake.java
 
 This program does the following:
   Reads in the names, populations and growth rates for two species
   and calculates the number of years it will take the one
   with the lower initial population to overtake the other
   or prints a message that it will never have a higher population).
 
 Preconditions: none.
 
 Postconditions:
   Displays either the number of years it takes for the species with the
   lower population to have a population greater than the other,
   or a message that it will not exceed the other
   species' population within ten years.
 
 Author: Lew Rakocy
 email address: Lrakocy@devrycols.edu
 Date: <2/20/99>
 Changes: 08/26/2000 Changed to the newer version of Species.
          02/23/2003 Made comments like text examples.
*/
public class YearsToOvertake
{
    public static void main(String[] args)
    {
       Species s1 = new Species();
       Species s2 = new Species(); 
       Species lower = new Species();  // has lower initial population
       Species higher = new Species(); // has higher initial population

       // Read in two species
       
       s1.readInput();
       s2.readInput();

       // Special condition detection:
       // What if they have the same initial populations?

       // Determine which has the lower initial population -
       // if they are the same,
       // assign the 1st species to "lower" and the 2nd to "higher"
       
       if(s1.getPopulation() <= s2.getPopulation())
       {
           lower.set(s1.getName(), s1.getPopulation(), s1.getGrowthRate());
           higher.set(s2.getName(), s2.getPopulation(), s2.getGrowthRate());
       }
       else
       {
           lower.set(s2.getName(), s2.getPopulation(), s2.getGrowthRate());
           higher.set(s1.getName(), s1.getPopulation(), s1.getGrowthRate());
       }

       // Find number of years it takes the species with lower initial
       // population to overtake the other one.
       
       int years = 1;
       int nextPopulationLower = lower.getPopulation();
       int nextPopulationHigher = higher.getPopulation();
       while (nextPopulationLower <= nextPopulationHigher && years <= 10)
       {
           nextPopulationLower = lower.projectedPopulation(years);
           nextPopulationHigher = higher.projectedPopulation(years);
           years++; // WARNING: exit value of years will be 1 too many
       }

       // Adjust years to correct value
       // (it is 1 too many on exit from while-loop)
       
       years--;

       // Be careful when years = 10:
       // "lower" may or may not have overtaken "higher"
       //      so you must do a final check:

       if(years == 10 && nextPopulationLower <= nextPopulationHigher)
       {
           System.out.println("Species " + lower.getName()
                 + " will not overtake " + higher.getName()+ ".");
       }
       else
       {
           System.out.println("Species " + lower.getName()
                            + " will overtake " + higher.getName()
                                           + " in " + years + " years.");
       }
    }
 }

